﻿#region Copyright

// /*
// SilverlightInEnterprise
// SilverlightInEnterprise
// XamRibbonAdapter.cs
// 
// Author: Matt Van Horn
// Created 11/16/2011 2:24 AM
// Copyright: Silverlight in the Enterprise www.slinenterprise.com
// 
// Licensed under MS-PL http://www.opensource.org/licenses/MS-PL and while you 
// may use this in anyway you see fit and owe us nothing to do so, we wouldn't mind knowing that you got value out of the work we did (so we will do more) so 
// we ask that you leave a comment on www.slinenterprise.com or on the codeplex site slinenterprise.codeplex.com
// 
// */

#endregion

using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using Infragistics.Controls.Menus;
using SilverlightInEnterprise.Interfaces.Ribbon;

namespace SilverlightInEnterprise.Controls
{
    public class XamRibbonAdapter : XamRibbon
    {
        #region RibbonTabs 

        public static readonly DependencyProperty RibbonTabsProperty =
            DependencyProperty.Register("RibbonTabs", typeof (ReadOnlyObservableCollection<IRibbonTab>),
                                        typeof (XamRibbonAdapter),
                                        new PropertyMetadata(RibbonTabsProperty_ChangedCallback));

        public ReadOnlyObservableCollection<IRibbonTab> RibbonTabs
        {
            get { return (ReadOnlyObservableCollection<IRibbonTab>) GetValue(RibbonTabsProperty); }
            set { SetValue(RibbonTabsProperty, value); }
        }

        /// <summary>
        ///   DP changed call back for RibbonTabs
        /// </summary>
        /// <param name = "obj">instance of XamRibbonAdapter that is having its RibbonTabs property changed</param>
        /// <param name = "e">the DP changed arguments</param>
        private static void RibbonTabsProperty_ChangedCallback(DependencyObject obj,
                                                               DependencyPropertyChangedEventArgs e)
        {
            XamRibbonAdapter element = obj as XamRibbonAdapter;

            element.OnRibbonTabsChanged(e);
        }

        /// <summary>
        ///   Method will enable classes that derive from XamRibbonAdapter to handle RibbonTabs changes
        /// </summary>
        /// <param name = "e">the DP changed arguments passed in to the DP changed callback</param>
        protected virtual void OnRibbonTabsChanged(DependencyPropertyChangedEventArgs e)
        {
            INotifyCollectionChanged changed = this.RibbonTabs;
            changed.CollectionChanged += new NotifyCollectionChangedEventHandler(changed_CollectionChanged);
            this.HandleTabs();
        }

        private void changed_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.HandleTabs();
        }

        private void HandleTabs()
        {
            this.Tabs.Clear();
            foreach (var ribbonTab in this.RibbonTabs)
            {
                XamRibbonTabItem tab = new XamRibbonTabItem()
                                           {
                                               Header = ribbonTab.DisplayName
                                           };

                foreach (var ribbonGroup in ribbonTab.Children)
                {
                    XamRibbonGroup group = new XamRibbonGroup()
                                               {
                                                   Caption = ribbonGroup.DisplayName
                                               };
                    ;
                    //     group.SetBinding(UIElement.IsEnabledProperty, "IsEnabled");
                    foreach (var ribbonTool in ribbonGroup.Children)
                    {
                        group.Tools.Add((ribbonTool as IRibbonTool).Tool);
                    }
                    tab.Groups.Add(group);
                }
                this.Tabs.Add(tab);
            }
        }

        #endregion

        public XamRibbonAdapter()
        {
        }
    }
}